package com.imddy.spbatch001.utils;

import java.util.HashMap;
import java.util.Map;


public class CommBo extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    public static final String STATUS_TAG = "status";
    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";


    public CommBo() {
    }

    public CommBo(Boolean status, String code, String msg, Object data) {
        super.put(STATUS_TAG, status);
        super.put(CODE_TAG, code); //基本不会使用，在多状态时使用
        super.put(MSG_TAG, msg);
        super.put(DATA_TAG, data);
    }


    public static CommBo fail() {
        return CommBo.fail(null, null, null);
    }

    public static CommBo fail(String msg) {
        return CommBo.fail(null, msg, null);
    }

    public static CommBo fail(String msg, Object data) {
        return CommBo.fail(null, msg, data);
    }

    public static CommBo fail(String code, String msg, Object data) {
        return new CommBo(false, code, msg, data);
    }



    public static CommBo success() {
        return CommBo.success(null, null, null);
    }

    public static CommBo success(String msg) {
        return CommBo.success(null, msg, null);
    }

    public static CommBo success(String msg, Object data) {
        return CommBo.success(null, msg, data);
    }

    public static CommBo success(Object data) {
        return CommBo.success(null, null, data);
    }

    public static CommBo success(String code, String msg, Object data) {
        return new CommBo(true, code, msg, data);
    }


    /**
     * 判断是否为成功状态。
     * - 默认（未设置）视为成功
     */
    public boolean isSuccess() {
        Object status = get(STATUS_TAG);
        return status == null || Boolean.TRUE.equals(status);
    }
    /**
     * 判断是否为失败状态。
     * - 默认（未设置）视为非失败(视为成功)
     */
    public boolean isFail() {
        Object status = get(STATUS_TAG);
        return status != null && Boolean.FALSE.equals(status);
    }

    public String getCode() {
        return (String) this.get(CODE_TAG);
    }
    public String getMsg() {
        return (String) this.get(MSG_TAG);
    }
    public Object getData() {
        return this.get(DATA_TAG);
    }



    public CommBo put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public CommBo putALL(Map<String, Object> map) {
        if (map != null) {
            for (Entry<String, Object> entry : map.entrySet()) {
                super.put(entry.getKey(), entry.getValue());
            }
        }
        return this;
    }


}
